//
//  LWPopMenueView.m
//  1.28  weibo
//
//  Created by mac on 15/1/31.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWPopMenueView.h"

@implementation LWPopMenueView

UIWindow *_window;
dismissBlock _dismiss;


+ (void)popFrom:(CGRect )fromRect  inView:(UIView *)inView cntent:(UIView *)content dismissBlock:(dismissBlock)dismiss{

    //1.创建window
    _dismiss = [dismiss copy];
    _window = [[UIWindow alloc] init];
    _window.frame = [UIScreen mainScreen].bounds;
    _window.hidden = NO;
    _window.windowLevel = UIWindowLevelAlert;
    
    //2.创建蒙版
    UIButton *cover = [[UIButton alloc] initWithFrame:[UIScreen mainScreen].bounds];
    cover.backgroundColor = [UIColor clearColor];
    [cover addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventTouchUpInside];
    [_window addSubview:cover];
    //3.创建菜单容器
    UIImageView *menueView = [[UIImageView alloc] init];
    menueView.userInteractionEnabled = YES;
    menueView.image = [UIImage imageNamed:@"popover_background"];
    [cover addSubview:menueView];
    content.x = 13;
    content.y = 20;
    [menueView addSubview:content];
    
    //4.设置菜单栏的frame
    CGFloat menueW = CGRectGetMaxX(content.frame) + content.x;
    CGFloat menueH = CGRectGetMaxY(content.frame) + content.y;
    menueView.frame = CGRectMake(0, 0, menueW, menueH);
    
    //把fromRect的坐标从父控件转化到window
    CGRect resultFrame = [_window convertRect:fromRect fromView:inView];
    menueView.y = CGRectGetMaxY(resultFrame);
    menueView.centerX = resultFrame.origin.x + resultFrame.size.width * 0.5;
    _window.hidden = NO;
    
}
+ (void)popFrom:(UIView *)fromView cntent:(UIView *)content dismissBlock:(dismissBlock)dismiss{
    
    [self popFrom:fromView.frame inView:fromView.superview cntent:content dismissBlock:dismiss];
   }
UIViewController *_contentVc;
+ (void)popFrom:(UIView *)fromView cntentVc:(UIViewController *)contentVc dismissBlock:(dismissBlock)dismiss{
    _contentVc = contentVc;
    [self popFrom:fromView cntent:_contentVc.view dismissBlock:dismiss];
}

#warning 不能用类方法
+ (void)coverClick:(UIButton *)btn{

    _window = nil;
    _contentVc = nil;
    if (_dismiss != nil) {
        _dismiss();
    }
}
@end
